<script>
import authManager from '@/utils/auth.js'

export default {
	onLaunch: function() {
		console.log('App Launch')
		
		// 应用启动时初始化认证管理器
		authManager.init()
		
		// 检查登录状态
		this.checkLoginStatus()
	},
	
	onShow: function() {
		console.log('App Show')
	},
	
	onHide: function() {
		console.log('App Hide')
	},
	
	methods: {
		/**
		 * 检查登录状态
		 */
		async checkLoginStatus() {
			try {
				if (authManager.isAuthenticated()) {
					// 验证登录状态是否有效
					const isValid = await authManager.checkLoginStatus()
					if (!isValid) {
						console.log('登录状态已失效，清除本地状态')
						authManager.clearAuth()
						this.redirectToLogin()
					}
				} else {
					// 未登录，跳转到登录页面
					this.redirectToLogin()
				}
			} catch (error) {
				console.error('检查登录状态失败:', error)
				this.redirectToLogin()
			}
		},
		
		/**
		 * 跳转到登录页面
		 */
		redirectToLogin() {
			// 获取当前页面路径
			const pages = getCurrentPages()
			const currentPage = pages[pages.length - 1]
			const currentRoute = currentPage ? currentPage.route : ''
			
			// 如果当前不在登录页面，则跳转到登录页面
			if (currentRoute !== 'pages/login/login') {
				console.log('未登录，跳转到登录页面')
				uni.reLaunch({
					url: '/pages/login/login'
				})
			}
		}
	}
}
</script>

<style>
/* 全局样式 */
@import url('./static/fonts/iconfont.css'); /* 图标字体文件 */

/* 重置默认样式 */
* {
	box-sizing: border-box;
}

page {
	background-color: #1e2433;
	color: #ffffff;
	font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Helvetica Neue', Arial, sans-serif;
}

/* 通用工具类 */
.flex {
	display: flex;
}

.flex-center {
	display: flex;
	align-items: center;
	justify-content: center;
}

.flex-between {
	display: flex;
	align-items: center;
	justify-content: space-between;
}

.flex-column {
	display: flex;
	flex-direction: column;
}

.text-center {
	text-align: center;
}

.text-left {
	text-align: left;
}

.text-right {
	text-align: right;
}

/* 颜色工具类 */
.text-primary {
	color: #4e5eff;
}

.text-secondary {
	color: #8a8a9a;
}

.text-success {
	color: #52c41a;
}

.text-warning {
	color: #faad14;
}

.text-error {
	color: #ff4757;
}

.bg-primary {
	background-color: #4e5eff;
}

.bg-secondary {
	background-color: #2a304d;
}

/* 间距工具类 */
.m-0 { margin: 0; }
.m-1 { margin: 10rpx; }
.m-2 { margin: 20rpx; }
.m-3 { margin: 30rpx; }

.mt-0 { margin-top: 0; }
.mt-1 { margin-top: 10rpx; }
.mt-2 { margin-top: 20rpx; }
.mt-3 { margin-top: 30rpx; }

.mb-0 { margin-bottom: 0; }
.mb-1 { margin-bottom: 10rpx; }
.mb-2 { margin-bottom: 20rpx; }
.mb-3 { margin-bottom: 30rpx; }

.p-0 { padding: 0; }
.p-1 { padding: 10rpx; }
.p-2 { padding: 20rpx; }
.p-3 { padding: 30rpx; }

/* 通用按钮样式 */
.btn {
	display: flex;
	align-items: center;
	justify-content: center;
	padding: 20rpx 40rpx;
	border-radius: 40rpx;
	font-size: 28rpx;
	transition: all 0.3s;
}

.btn-primary {
	background-color: #4e5eff;
	color: #ffffff;
}

.btn-secondary {
	background-color: #2a304d;
	color: #ffffff;
}

.btn-outline {
	background-color: transparent;
	border: 2rpx solid #4e5eff;
	color: #4e5eff;
}

.btn-disabled {
	background-color: #2a2a2a;
	color: #666666;
	opacity: 0.6;
}

/* 通用卡片样式 */
.card {
	background-color: #2a304d;
	border-radius: 20rpx;
	padding: 30rpx;
	margin-bottom: 20rpx;
}

.card-header {
	display: flex;
	align-items: center;
	justify-content: space-between;
	margin-bottom: 20rpx;
}

.card-title {
	font-size: 32rpx;
	font-weight: bold;
	color: #ffffff;
}

.card-content {
	color: #ffffff;
}

/* 输入框通用样式 */
.input-group {
	display: flex;
	align-items: center;
	background-color: #2a304d;
	border-radius: 50rpx;
	padding: 20rpx 30rpx;
	margin-bottom: 30rpx;
	height: 90rpx;
}

.input-field {
	flex: 1;
	color: #ffffff;
	font-size: 32rpx;
	background-color: transparent;
	border: none;
}

.input-label {
	color: #ffffff;
	font-size: 32rpx;
	margin-right: 20rpx;
	min-width: 80rpx;
}

/* 加载动画 */
.loading {
	display: flex;
	align-items: center;
	justify-content: center;
	padding: 40rpx;
}

.loading-spinner {
	width: 40rpx;
	height: 40rpx;
	border: 4rpx solid #2a304d;
	border-top: 4rpx solid #4e5eff;
	border-radius: 50%;
	animation: spin 1s linear infinite;
}

@keyframes spin {
	0% { transform: rotate(0deg); }
	100% { transform: rotate(360deg); }
}

/* 错误提示样式 */
.error-tip {
	color: #ff4757;
	font-size: 24rpx;
	margin-top: -10rpx;
	margin-bottom: 20rpx;
	padding-left: 10rpx;
}

.success-tip {
	color: #52c41a;
	font-size: 24rpx;
	margin-top: -10rpx;
	margin-bottom: 20rpx;
	padding-left: 10rpx;
}

/* 响应式设计 */
@media screen and (max-width: 750rpx) {
	.container {
		padding: 0 20rpx;
	}
}

/* 暗黑模式适配 */
@media (prefers-color-scheme: dark) {
	page {
		background-color: #1e2433;
		color: #ffffff;
	}
}

/* 安全区域适配 */
.safe-area-inset-top {
	padding-top: constant(safe-area-inset-top);
	padding-top: env(safe-area-inset-top);
}

.safe-area-inset-bottom {
	padding-bottom: constant(safe-area-inset-bottom);
	padding-bottom: env(safe-area-inset-bottom);
}

/* 滚动条样式 */
::-webkit-scrollbar {
	width: 6rpx;
}

::-webkit-scrollbar-track {
	background: #2a304d;
}

::-webkit-scrollbar-thumb {
	background: #4e5eff;
	border-radius: 3rpx;
}

::-webkit-scrollbar-thumb:hover {
	background: #3a4eff;
}
</style>